<template>
	<div>
		<l-header :headerText='headerText'></l-header>
	<form class="modifypassword">
      <input class="input" type="password" name="username" v-model="oldpassword" placeholder="当前密码">
  		<input class="input" type="password" name="password" v-model="newpassword" placeholder="新密码" @blur="testPassword">
      <input class="input" type="password" name="password" v-model="confirmpassword" placeholder="确认新密码">
			<input type="button" name="button" class="button" @click="loginFormat" value="提 交"/>
			<p class="title">*密码需为6-18位的数字或字母</p>
	</form>
		</div>
</template>
<script>
import LHeader from '@/components/header/Header'
import axios from 'axios'
import { baseUrl } from '@/assets/js/var.js'
export default {
	name: 'modifypassword',
	data (){
		return{
			oldpassword:'',
			newpassword:'',
			confirmpassword:'',
			token:'',
			headerText: '修改密码',
			number:'',//工号
			check:true
		}
	},
	components:{
		 LHeader
	},
	methods:{
    //数据加密
		loginFormat (){
			if (this.check) {
				if (this.oldpassword && this.newpassword && this.confirmpassword ) {
		  		if (this.newpassword == this.confirmpassword ) {
						this.check = false
		  			axios.get(`${baseUrl}/api/v1.1/washCleanerMember/rsaKey`)
		  				.then(response =>{
								this.check = true
		  					if (response.data.code === 7001000) {
		  						let pwdKey = new RSAUtils.getKeyPair(response.data.data.exponent,"",response.data.data.modulus);
		              let oldPwd = this.oldpassword.split("").reverse().join("");
		              let newPwd = this.newpassword.split("").reverse().join("");
		  						let oldencrypedPwd = RSAUtils.encryptedString(pwdKey,oldPwd);
		              let newencrypedPwd = RSAUtils.encryptedString(pwdKey,newPwd);
		  						this.oldpassword = oldencrypedPwd.toString()
		              this.newpassword = newencrypedPwd.toString()
		  						this.submit()
		  					}else {
		  						alert('服务器繁忙，请稍后重试')
		  					}
		  				})
		  				.catch(error => {
								this.check = true
		  					console.log(error);
		  				})
		        }else{
		          alert("新密码输入不一致，请确认！")
		          this.newpassword = ""
		  				this.confirmpassword = ""
		        }
		      }else{
		        alert("请填写相应信息！")
		      }
			}else{}

		},
    //修改密码
		submit (){
		    const qs = require('qs')
				let param = qs.stringify({
					'number': this.number,
					'oldPassword': this.oldpassword,
					'newPassword': this.newpassword
				})
				axios.post(`${baseUrl}/api/v1.1/washCleaner/updatePassword`,param)
				.then(response =>{
					let data = response.data
					if (data.code === 7001000) {
						alert('修改成功！');
						this.$router.push('/');
					}else if (data.code === 7001001) {
						alert('修改失败，请稍后尝试。');
					}else if (data.code === 7001010) {
            alert('未登录或登录时间超时');
						this.$router.replace('/')
          }else if (data.code === 7001012) {
            alert('服务器繁忙，请稍后重试');
          }else if (data.code === 7001013){
						alert('密码不能为空')
					}else if (data.code === 7001014) {
            alert('旧密码错误,请重试');
            this.oldpassword = ""
            this.newpassword = ""
            this.confirmpassword = ""
          }else{
						alert('未知错误')
					}
				})
				.catch(function(error){
					console.log(error);
				})
	},
	testPassword (){
		let reg = /^[a-zA-Z0-9]{6,18}$/;
				if(reg.test(this.newpassword)){

				}else {
					alert("密码应为6-18位的数字或字母")
					this.newpassword = ""
				}
	},
	//从localStorage中获取员工的工号number
	getCleanerId (){
		if (localStorage.waterUserInfo) {
			const obj =  JSON.parse(localStorage.waterUserInfo)
			return obj.number
		}else{
			alert('登录超时')
			this.$router.push('/')
		}
	}
},
mounted (){
	this.number = this.getCleanerId()
}
}
</script>
<style  lang="stylus" scoped>
	.modifypassword
		width: 100%
		text-align: center
		margin-top: 1rem
		.input
			width:90%
			height: .8rem
			border: 1px solid #ccc
			background: white
			margin: .1rem
			border-radius: .05rem
			-webkit-appearance:none
			line-height: normal;
			outline:none;
			background: transparent;
			padding-left: .2rem
		.input:focus
			border: 1px solid red
		.input::-webkit-input-placeholder
			color: #ccc
		.button
			-webkit-appearance: none
			width: 2.14rem
			height: .6rem
			background: #66cc66
			color:white
			font-size: 20px
			border-radius: .2rem
			margin-top: .2rem
			box-shadow:0rem 1px 1px #000
		.title
			color: #ccc
			margin-top: .2rem

</style>
